<template>
  <panel title="订单管理">
    <el-form :inline="true" label-width="80px" :model="searchInfo">
      <el-form-item label="订单号">
        <el-input v-model="searchInfo.orderNo"></el-input>
      </el-form-item>
      <el-form-item label="收货人">
        <el-input v-model="searchInfo.consignee"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="searchInfo.phone"></el-input>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="searchInfo.orderState">
          <el-option value="已受理" label="已受理"></el-option>
          <el-option value="派送中" label="派送中"></el-option>
          <el-option value="已完成" label="已完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择时间">
        <el-date-picker v-model="searchInfo.date" type="datetimerange" :picker-options="pickerOptions"
          range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="orderList">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="orderLists">
      <el-table-column label="订单号" prop="orderNo"></el-table-column>
      <el-table-column label="下单时间">
        <template slot-scope="{row}">
          {{ row.orderTime | formDate }}
        </template>
      </el-table-column>
      <el-table-column label="联系电话" prop="phone"></el-table-column>
      <el-table-column label="收货人" prop="consignee"></el-table-column>
      <el-table-column label="送货地址" prop="deliverAddress"></el-table-column>
      <el-table-column label="送达时间" prop="deliveryTime">
        <template slot-scope="{row}">
          {{ row.deliveryTime | formDate }}
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remarks"></el-table-column>
      <el-table-column label="订单金额" prop="orderAmount"></el-table-column>
      <el-table-column label="订单状态" prop="orderState"></el-table-column>
      <el-table-column label="操作">
        <template>
          <el-button type="text" size="small">查看</el-button>
          <el-button type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="pageInfo.currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @current-change="pageChange"
      @size-change="sizeChange">
    </el-pagination>
  </panel>
</template>

<script>
import { orderListReq } from '@/api/order.api'
import dayjs from 'dayjs';
import { clearObject } from '@/utils/common'
export default {
  data() {
    return {
      // 时间的快捷方式
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 页数和每一页数据的数量
      pageInfo: {
        currentPage: 1,
        pageSize: 5
      },
      // 查询条件的对象
      searchInfo: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: ''
      },
      // 订单列表
      orderLists: [],
      // 查询订单的状态
      // orderState: [],
      // 订单总数
      total: null
    }
  },
  methods: {
    // 分页设置
    pageChange(currentPage) {
      this.pageInfo.currentPage = currentPage
      this.orderList()
    },

    // 一页的数据的数量
    sizeChange(pageSize) {
      this.pageInfo.pageSize = pageSize
      this.orderList()

    },
    // 查询订单
    // 封装请求订单列表接口函数
    orderList() {
      let parmas = { ...this.pageInfo, ...this.searchInfo }
      // // 数据清洗
      // console.log(parmas.date);

      clearObject(parmas)
      if (parmas.date) {
        // 处理时间格式
        parmas.date = JSON.stringify(parmas.date.map(item => dayjs(item).format('YYYY-MM-DD HH:mm:ss')))
      }
      orderListReq(parmas).then(res => {
        // 将得到的数据赋值给orderLists
        console.log(res);
        this.orderLists = res.data.data
        // 订单总数
        this.total = res.data.total
      })
    }
  },
  // 一进页面得到数据
  created() {
    this.orderList()
  },
  // 修改日期格式
  filters: {
    formDate(orderTime) {
      return dayjs(orderTime).format('YYYY-MM-DD HH:mm:ss')
    }
  }
};
</script>

<style lang="less" scoped>

</style>